package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.query.StrategyDetailQuery;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IStrategyCommendService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.IStrategyTagService;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisVOService;
import cn.wolfcode.luowowo.cache.vo.StrategyStatisVO;
import cn.wolfcode.luowowo.comment.domain.StrategyComment;
import cn.wolfcode.luowowo.comment.service.IStrategyCommentService;
import cn.wolfcode.luowowo.comment.query.StrategyCommentQuery;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.search.query.StrategySearchQuery;
import cn.wolfcode.luowowo.search.serivce.IStrategySearchService;
import cn.wolfcode.luowowo.search.vo.StatisVO;
import cn.wolfcode.luowowo.website.web.annotation.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

//攻略的controller
@Controller
@RequestMapping("/strategy")
public class StrategyController {

    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private IDestinationService destinationService;

    @Reference
    private IStrategyTagService strategyTagService;

    @Reference
    private IStrategyCommentService strategyCommentService;

    @Reference
    private IStrategyStatisVOService strategyStatisVOService; //统计

    @Reference
    private IStrategyCommendService strategyCommendService; //推荐

    @Reference
    private IStrategySearchService strategySearchService;

    //推荐首页
    @RequestMapping("")
    public String index(Model model){

        model.addAttribute("commends",strategyCommendService.getTop5());

        //共享热门排行
        List<StrategyStatisVO> hotCds = strategyStatisVOService.queryHotCommends();
        model.addAttribute("hotCds",hotCds);

        //国内
        List<StrategyStatisVO> unabroadCds = strategyStatisVOService.queryChinaCommends();
        model.addAttribute("unabroadCds",unabroadCds);

        //国外
        List<StrategyStatisVO> abroadCds = strategyStatisVOService.queryAbroadCommends();
        model.addAttribute("abroadCds",abroadCds);

        //主题推荐
        //themeCds
        List<Map<String,Object>> list = strategySearchService.queryThemeCommend();
        model.addAttribute("themeCds",list);

        //主题攻略条件列表
        List<StatisVO> themes = strategySearchService.queryConditionThemes();
        model.addAttribute("themes",themes);

        //国内条件列表:chinas
        List<StatisVO> chinas = strategySearchService.queryConditionProvinces();
        model.addAttribute("chinas",chinas);

        //国外条件列表:abroads
        List<StatisVO> abroads = strategySearchService.queryConditionCountries();
        model.addAttribute("abroads",abroads);

        return "strategy/index";
    }

    //首页条件查询
    @RequestMapping("/searchPage")
    public  String searchPage(Model model, @ModelAttribute("qo")StrategySearchQuery qo){

        //page
        model.addAttribute("page", strategySearchService.query(qo));

        return "strategy/searchPageTpl";
    }

    @RequestMapping("/detail")
    public String detail(Model model,Long id){

        StrategyDetail detail = strategyDetailService.get(id);
        detail.setStrategyContent(strategyDetailService.getContent(id));

        model.addAttribute("detail",detail);

        //统计的阅读数                          每次访问都会增加的数量
        strategyStatisVOService.increaseViewnum(id,1);

        //vo对象
        model.addAttribute("vo",strategyStatisVOService.getStrategyStatisVO(id));

        //热门排行,阅读加分
        strategyStatisVOService.addHotScore(id,1);

        return "strategy/detail";
    }

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo")StrategyDetailQuery qo){

        //toasts
        List<Destination> toasts = destinationService.getToasts(qo.getDestId());
        model.addAttribute("toasts", toasts);

        //dest 拿到最后一个目的地
        Destination dest = toasts.remove(toasts.size() - 1);
        model.addAttribute("dest", dest);

        //tags
        model.addAttribute("tags",strategyTagService.list());

        //pageInfo
        model.addAttribute("pageInfo",strategyDetailService.query(qo));

        return "strategy/list";
    }

    //攻略的评论
    @RequestMapping("/commentAdd")
    @ResponseBody
    public Object commentAdd(StrategyComment comment, @UserParam UserInfo userInfo){

        comment.setCity(userInfo.getCity());
        comment.setHeadUrl(userInfo.getHeadImgUrl());
        comment.setUserId(userInfo.getId());
        comment.setUsername(userInfo.getNickname());
        comment.setLevel(userInfo.getLevel());

        strategyCommentService.save(comment);

        //评论数+1                                 //攻略的id,增加的数量
        strategyStatisVOService.increaseReplynum(comment.getDetailId(),1);

        //根据攻略id,返回该攻略的最大评论数
        int replynum = strategyStatisVOService.getStrategyStatisVO(comment.getDetailId()).getReplynum();

        //热门排行,评论加分
        strategyStatisVOService.addHotScore(comment.getDetailId(),1);

        //暂时使用一个常量,后面用redis解决
        return AjaxResult.success().addData(replynum);
    }

    //攻略评论的列表
    @RequestMapping("/comment")
    public String comment(Model model,@ModelAttribute("qo") StrategyCommentQuery qo){

        Page page = strategyCommentService.query(qo);
        model.addAttribute("page",page);

        return "strategy/commentTpl";
    }

    //攻略评论点赞
    @RequestMapping("/commentThumbUp")
    @ResponseBody                   //评论的id     用户的id
    public Object commentThumbUp(String toid,Long fromid){

        strategyCommentService.commentThumbUp(toid,fromid);

        return AjaxResult.success();
    }

    //收藏
    @RequestMapping("/favor")
    @ResponseBody
    public Object favor(Long sid, @UserParam UserInfo userInfo){
        //没有登录的情况
        if(userInfo == null){
            AjaxResult result = new AjaxResult(false, "请先登录");
            result.setCode(AjaxResult.NO_LOGIN);
            return result;
        }

        //判断收藏还是取消
        boolean ret = strategyStatisVOService.favor(sid,userInfo.getId());

        //国内国外排行加减分
        strategyStatisVOService.addCommendScore(sid,ret?1:-1);

        return new AjaxResult(ret,"").addData(strategyStatisVOService.getStrategyStatisVO(sid));
    }

    //顶
    @RequestMapping("/strategyThumbup")
    @ResponseBody
    public Object strategyThumbup(Long sid, @UserParam UserInfo userInfo){
        //没有登录的情况
        if(userInfo == null){
            AjaxResult result = new AjaxResult(false, "请先登录");
            result.setCode(AjaxResult.NO_LOGIN);
            return result;
        }

        boolean ret = strategyStatisVOService.strategyThumbup(sid,userInfo.getId());

        //国内国外排行加减分
        if(ret){
            strategyStatisVOService.addCommendScore(sid,1);
        }

        return new AjaxResult(ret,"").addData(strategyStatisVOService.getStrategyStatisVO(sid));
    }

}
